package com.lelefly.base.daoimp;

import java.util.List;

import org.hibernate.Hibernate;
import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cache.ehcache.internal.util.HibernateUtil;
import org.hibernate.transform.Transformers;
import org.springframework.stereotype.Repository;

import com.lelefly.base.model.LeleFlyHistoryDetailBean;
import com.lelefly.base.po.LeleFlyHistory;
import com.lelefly.base.po.LeleFlyInfo;
import com.lelefly.base.po.LeleflyHistoryInfo;
import com.lelefly.base.po.LeleflyOrganizationInfo;
import com.lelefly.db.dbmanager.HibernateSessionFactory;

@Repository
public class LeleFlyInfoDao extends BaseDao{

	public LeleFlyInfo getLastTimeFly(String hql){
		
		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		try {
//			Query query = session.createQuery(hql);
			List sqlQuery = session.createSQLQuery(hql).addScalar("flightime").list();
			return null;
		} catch (Exception e) {
			// TODO: handle exception
			getLogger().info("----->>>" + e.getMessage());
			return null;
		} finally {
			session.close();
		}
	}
	
	
	public List<LeleflyHistoryInfo> queryHistoryInfos(String hql,int page,int count){
		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		
		try {
			Query query = session.createQuery(hql);
			query.setFirstResult((page-1)*count);
			query.setMaxResults(count);
			
			return query.list();
		} catch (Exception e) {
			getLogger().info("----->>>" + e.getMessage());
			return null;
		}finally{
			session.close();
		}
	}
	
	
	public List<LeleFlyHistoryDetailBean> queryFlyDetail(String sql){
		SessionFactory sessionFactory = HibernateSessionFactory
				.getSessionFactory();
		Session session = sessionFactory.openSession();
		
		try {
			return session.createSQLQuery(sql)
					.addScalar("planeid")
					.addScalar("flightime")
					.addScalar("mobilePhoneNo")
					.addScalar("userPhoneNo")
					.addScalar("latitude")
					.addScalar("longitude")
					.addScalar("flightmode")
					.addScalar("radarHeight")
					.addScalar("barometerHeight")
					.addScalar("speed")
					.addScalar("flowData")
					.addScalar("gpsNo")
					.addScalar("radio")
					.addScalar("battVolt")
					.addScalar("historyId")
					.setResultTransformer(
					Transformers.aliasToBean(LeleFlyHistoryDetailBean.class)).
					list();
		} catch (Exception e) {
			getLogger().error("---->" + e.getMessage());
			return null;
		}finally {
			session.close();
		}
	}
}
